<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="预计总工时" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="expectTotalHours">
              <a-input :disabled=formDisabled   v-model="model.expectTotalHours" placeholder="请输入预计总工时" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="预计效能分" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="expectedEfficacy">
              <a-input :disabled=formDisabled   v-model="model.expectedEfficacy" placeholder="请输入预计效能分" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="任务编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskNo">
              <a-input  v-model="model.taskNo" placeholder="请输入任务编码"   disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="任务级别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskLevel">
              <j-dict-select-tag :disabled=formDisabled type="list"  disabled v-model="model.taskLevel" dictCode="task_level" placeholder="请选择任务级别" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="任务名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskName">
              <a-input :disabled=formDisabled v-model="model.taskName" placeholder="请输入任务名称"  ></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="任务责任人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskExecUser">
              <select-user-by-dep
                placeholder="请选择任务责任人"
                :disabled="formDisabled"
                v-model="model.taskExecUser" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="任务进度模板" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isChoiseTemplate">
              <select-process-template-input v-model="model.templateId" placeholder="请输入进度模板"  :multi = false />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="匹配合同额（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractAmount">
              <a-input :disabled=formDisabled v-model="model.contractAmount" placeholder="请输入匹配合同额"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="任务说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskDesc">
              <a-textarea
                :disabled="formDisabled"
                v-model="model.taskDesc"
                rows="6"
                placeholder="请输入任务说明"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import SelectProcessTemplateInput from '@/views/scy/components/SelectProcessTemplateInput'
  import SelectPersonInput from '@/views/scy/components/SelectPersonInput'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import { mapGetters } from 'vuex';
  export default {
    name: 'ErpProgressAssignedTaskForm',
    components: {
      SelectProcessTemplateInput,
      SelectPersonInput,
      SelectUserByDep
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          assignedTask: '/api/erp/progress/erpProgressTask/assignedTask',
          queryById: '/api/erp/progress/erpProgressTask/queryById',
          generatedFormNumber: '/api/erp/base/generatedFormNumber'
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      generatedFormNumber() {
        getAction(this.url.generatedFormNumber, { prefix: 'KH', subSystemCode: '05' }).then(res => {
          if (res.success) {
            this.model.taskNo = res.result
          }
        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs.form.resetFields();
        this.model = Object.assign({}, record);
        //todo 、下期迭代
        if (record.taskLevel && record.taskLevel !== '4') {
          var level = record.taskLevel;
          level = parseInt(level) + 1;
          this.model.taskLevel = level + '';
          console.log('this.model.taskLevel', this.model.taskLevel)
        }

        this.model.isChoiseTemplate = ''
        this.model.templateId = ''
        if (!this.model.taskNo) {
          this.generatedFormNumber();
        }
        this.visible = true;
      },
      ...mapGetters(['userInfo']),
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = this.url.assignedTask;
            let method = 'post';
            this.model.assignedBy = this.userInfo().empId; //任务委托人
            this.model.taskLeading = this.model.taskExecUser; // 任务执行人
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>
